package com.tsd.base.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.core.constants.Constants;
import com.tsd.base.entity.BaseInvoiceInfo;
import com.tsd.base.entity.BaseInvoiceInfoExt;
import com.tsd.base.service.BaseInvoiceInfoService;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysUser;
import com.tsd.system.utils.AuthViewFieldUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseInvoiceInfo发票信息 控制层
 *
 * @author Hillpool
 * @date 2022/11/04 18:14:03
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "基础资料-发票信息", description = "发票信息的API描述")
public class BaseInvoiceInfoController extends BaseController {

    @Resource
    private BaseInvoiceInfoService baseInvoiceInfoService;

    @ApiOperation(value = "根据sid获取发票信息", notes = "根据sid获取发票信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "发票信息sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getBaseInvoiceInfoBySid.action", method = RequestMethod.POST)
    public HttpResult<BaseInvoiceInfo> getBaseInvoiceInfoBySid() {
        HttpResult<BaseInvoiceInfo> hr = new HttpResult<BaseInvoiceInfo>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            BaseInvoiceInfo baseInvoiceInfo = baseInvoiceInfoService.queryBySid(sid);
            hr.setData(baseInvoiceInfo);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除发票信息", notes = "根据id删除发票信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "发票信息sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteBaseInvoiceInfos.action", method = RequestMethod.POST)
    public HttpResult<String> deleteBaseInvoiceInfos() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            baseInvoiceInfoService.deleteBaseInvoiceInfos(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存发票信息", notes = "保存发票信息，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "发票信息", required = true, paramType = "body", dataType = "BaseInvoiceInfo")
    })
    @ResponseBody
    @RequestMapping(value = "/saveBaseInvoiceInfo.action", method = RequestMethod.POST)
    public HttpResult<Object> saveBaseInvoiceInfo() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            BaseInvoiceInfo t = JSON.parseObject(json, BaseInvoiceInfo.class);
            baseInvoiceInfoService.saveBaseInvoiceInfo(t, opUser);
            hr.setData(t);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询发票信息", notes = "分页查询发票信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findBaseInvoiceInfos.action", method = RequestMethod.POST)
    public HttpResult<Object> findBaseInvoiceInfos() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);

            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "owner_sid", "owner_sid", false, false);
            HlpUtils.copyStringValue(map, params, "entity_name", "entity_name", false, false);
            HlpUtils.copyStringValue(map, params, "entity_code", "entity_code", false, false);
            HlpUtils.copyStringValue(map, params, "entity_phone", "entity_phone", false, false);
            HlpUtils.copyStringValue(map, params, "entity_bank", "entity_bank", false, false);
            HlpUtils.copyStringValue(map, params, "entity_bank_account", "entity_bank_account", false, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            if (map.containsKey(Constants.KEY_NO_PAGE)) {
                page = null;
            }
            List<BaseInvoiceInfoExt> list = baseInvoiceInfoService.findBaseInvoiceInfos(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据owner_sid获取发票信息", notes = "根据owner_sid获取发票信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "owner_sid", value = "发票信息owner_sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getBaseInvoiceInfoByOwnerSid.action", method = RequestMethod.POST)
    public HttpResult<BaseInvoiceInfoExt> getBaseInvoiceInfoByOwnerSid() {
        HttpResult<BaseInvoiceInfoExt> hr = new HttpResult<BaseInvoiceInfoExt>();
        try {
            Map<String, Object> map = super.getParams(request);
            String owner_sid = HlpUtils.getString(map, "owner_sid");
            Integer dr = HlpUtils.getInteger(map, "dr", 0);
            super.checkEmpty(owner_sid, "请提供参数owner_sid");
            BaseInvoiceInfoExt baseInvoiceInfo = baseInvoiceInfoService.queryByOwnerSid(owner_sid, dr);
            if (!"-1".equals(owner_sid)) {
                SysUser opUser = super.getSysUser();
                AuthViewFieldUtil.clearAuthViewField(AuthViewFieldUtil.DATA_AUTH_CODE_CLIENT, baseInvoiceInfo, opUser);
            }
            hr.setData(baseInvoiceInfo);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
